/**
 * ScreenItemTag.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util.taglib;

import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.site.dto.backend.system.AuthorityBizBean;
import com.site.util.StringUtils;
import com.site.util.base.dto.User;
import com.site.util.constant.Constants;


/**
 * 权限控制
 * @author
 */
public class AuthTag extends TagSupport {
	
	private static final long serialVersionUID = 3702043313455975290L;
	/**
	 * 权限名称
	 */
	private String name;
	@Override
	public int doStartTag() throws JspException {
		if(!StringUtils.isEmpty(name)){
			if(hasAuth(name)){
				return EVAL_BODY_INCLUDE;
			}
		}
		return SKIP_BODY;
	}

	private boolean hasAuth(String authName)
	{
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		HttpSession session=(HttpSession)request.getSession();
		User user=(User)session.getAttribute(Constants.LOGIN_USER_BACKEND);
		if(user==null)
			return false;
		Set<AuthorityBizBean> auths=user.getAuthSet();
		for(AuthorityBizBean auth:auths) {
			if(auth.getName().equalsIgnoreCase(authName)) {
				return true;
			}
		}
		return false;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

}
